<template>
  <section>
    <el-row style="display: flex;align-items: center;">
      <el-col :span="6">
        <el-input size="mini" placeholder="请输入关键字"  clearable v-model="searchKey" @keydown.native.prevent.enter="searchBtn" ></el-input>
      </el-col>
      <el-col :span="4">
        <i class="el-icon-search" @click="searchBtn" style="margin-left:10px" ></i>
      </el-col>
      <el-col :span="12" >
        <el-button size="mini" @click="addRecipe" style="float: right" type="primary">新建食品功效</el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-table :data="recipeList" stripe v-loading="loading">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="制作方法：">
                <TextEditor v-if="props.row.edit" v-model="props.row.method" :editorToolbar="customToolbar"></TextEditor>
                <!--<el-input v-show="props.row.edit" size="small" v-model="props.row.description"></el-input>-->
                <span v-if="!props.row.edit" v-html="props.row.method"></span>
              </el-form-item>
              <el-form-item label="描述：">
                <el-input v-show="props.row.edit" size="mini" v-model="props.row.description"></el-input>
                <span v-show="!props.row.edit">{{props.row.description}}</span>
              </el-form-item>
              <el-form-item label="备注：">
                <el-input v-show="props.row.edit" size="mini" v-model="props.row.data.ps"></el-input>
                <span v-show="!props.row.edit">{{props.row.data.ps}}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号"  width="80px"></el-table-column>
        <el-table-column prop="name" label="名称">
          <template slot-scope="scope">
            <el-input v-show="scope.row.edit" size="mini" v-model="scope.row.name"></el-input>
            <span  v-show="!scope.row.edit">{{scope.row.name}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="code" label="配方" >
          <template slot-scope="scope">
            <el-input v-show="scope.row.edit" size="mini" v-model="scope.row.formula"></el-input>
            <span  v-show="!scope.row.edit">{{scope.row.formula}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="功效">
          <template slot-scope="scope">
            <el-input v-show="scope.row.edit" size="mini" v-model="scope.row.effect"></el-input>
            <span  v-show="!scope.row.edit">{{scope.row.effect}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="documentId" label="文件Id">
          <template slot-scope="scope">
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button :type="scope.row.edit?'success':'primary'" @click='confirm(scope.row)' size="mini" icon="edit">{{scope.row.edit?'完成':'编辑'}}</el-button>
            <el-button @click='cancelBtn(scope.row)' size="mini" v-if="scope.row.edit">取消</el-button>
            <el-button @click='del(scope)' size="mini" v-if="!scope.row.edit">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination v-show="totalPage>0" style="margin-top:20px;"
                     background
                     layout="prev, pager, next"
                     :page-count="totalPage"
                     @current-change="goPage">
      </el-pagination>
    </el-row>
  </section>
</template>

<script>
  import TextEditor from '@/components/TextEditor'
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import Vue from 'vue'//后续添加属性，需要vue联动
  import errHandler from '@/assets/errHandler'
  export default {
    name: "recipe",
    components:{TextEditor},
    data() {
      return {
        customToolbar: [['bold','italic','underline'],[{'list':'ordered'},{'list':'bullet'}],['image','code-block']],
        recipeList:[],
        totalPage:0,
        loading:false,
        searchKey:null
      }
    },
    created(){
      this.getRecipeList()
    },
    computed:{
    },
    watch: {
      searchKey(nv,ov){
        if(nv === ''){
          this.getRecipeList()
        }
      }
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      addMethod(row) {
        console.log(row)
        if(row.method === undefined){
          Vue.set(row,'method',[])
        }
        row.method.push('')
      },
      searchBtn() {
        this.getRecipeList('', this.searchKey)
      },
      goPage(val) {
        this.getRecipeList(val - 1)
      },
      getRecipeList(pageNo, key) {
        this.fetch({name: "getRecipeList", data: {page: pageNo ? pageNo : 0, key: key ? key : ''}})
          .then(res => {
            console.log(res)
            this.recipeList = res.content
            this.totalPage = res.totalPages
          })
          .catch(err => {
            console.log(err)
            errHandler.handle(this, err)
          })
      },
      addRecipe() {
        //新建一个空的food，表格页面+1栏
        this.fetch({name: "createRecipe", data: {name: ''}})
          .then(res => {
            console.log(res)
            res.edit = true
            this.recipeList.unshift(res)
          })
          .catch(err => {
            console.log(err)
            errHandler.handle(this, err)
          })
      },
      confirm(row) {
        console.log(row)
        if (row.edit === undefined) {
          Vue.set(row, 'edit', true)//后续添加属性，需要vue联动
          return
        }
        if (row.edit) {
          // row.method=JSON.stringify(row.method)
          this.loading = true
          this.updateRecipe(row)
        } else {
          row.edit = true
        }
      },
      cancelBtn(row) {//取消按钮
        if (row.edit === undefined) {
          Vue.set(row, 'edit', false)//后续添加属性，需要vue联动
        } else {
          row.edit = false
        }
      },
      del(scope) {//删除按钮
        this.loading = true
        this.$confirm('确认删除？')
          .then(() => {
            this.fetch({name: "delRecipe", data: {id: scope.row.id}})
              .then(res => {
                this.loading = false
                this.recipeList.splice(scope.$index, 1)
              })
              .catch(err => {
                console.log(err)
                errHandler.handle(this, err)
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
            this.loading = false
          })
      },
      updateRecipe(row) {
        this.fetch({name: "updateRecipe", data: {...row}})
          .then(res => {
            this.loading = false
            console.log(res)
            row.edit = false
          })
          .catch(err => {
            console.log(err)
            errHandler.handle(this, err)
          })
      }
    }
  }
</script>

<style scoped>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>
